#!/usr/bin/env bash
{ set +x; } 2>/dev/null 

IFS=
[[ -n ${#BASH_SOURCE[@]} ]] && [[ $((${#BASH_SOURCE[@]}*$SHLVL)) == 1 ]] && {
	{ set -x; cd "${BASH_SOURCE[0]%/*/*/*/*/*}"; { set +x; } 2>/dev/null; }
}

! [ -e setup.py ] && echo "ERROR: setup.py NOT EXISTS" && exit 0

which_pip="$(which pip)"
[[ -z "$which_pip" ]] && echo "SKIP: pip NOT INSTALLED" && exit 0

[ -d dist ] && { ( set -x; rm -fr dist ) || exit; }

# 1) generate .tar.gz
( set -x; python setup.py -q sdist ) || exit

find="$(find dist -type f -name "*.tar.gz")"
[[ -z "$find" ]] && echo "ERROR: dist/ EMPTY" && exit 1
# 2) pip install .tar.gz
# site-packages/ must be writable
# OSError: [Errno 1] Operation not permitted: site-packages/mod.py (python2)
# PermissionError: [Er=rno 1] Operation not permitted (python3)
# 	archives cached in ~/Library/Caches/pip/ (OSX)
# 	fix:
# 	a) pip --no-cache-dir install pkg.tar.gz (pip6+)
# 	b) sudo pip install pkg.tar.gz
( set -x; python "$which_pip" -q install --no-cache-dir "$find" ) || exit
[ -d dist ] && { ( set -x; rm -fr dist ) || exit; }
# pkg install site-packages/ with tmp name 'pip_*_build-*.egg-info'
# `rm -R` it after running tests 
:

